/**
 * 项目名：phms3
 * 日期：    2019-11-22 17:31:07
 * Copyright (c) 2015- HTSC-版权所有
 */

package com.joysuch.wwyt.mh.repository;

import com.joysuch.wwyt.mh.entity.MhMonitoringType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import javax.persistence.Tuple;
import java.util.List;

/**
 * MhMonitoringTypeDao
 */
public interface MhMonitoringTypeDao
        extends JpaRepository<MhMonitoringType, Long>, QuerydslPredicateExecutor<MhMonitoringType> {

    public Page<MhMonitoringType> findAll(Specification<MhMonitoringType> spec, Pageable pageable);

    public MhMonitoringType save(MhMonitoringType bean);

    public void delete(MhMonitoringType bean);

    public MhMonitoringType findFirstByName(String name);

    @Query(nativeQuery = true, value = "select * from wwyt_mh_monitoring_type where name like %?1% ")
    public List<MhMonitoringType> findByNameContains(String name);

    @Query("select id from MhMonitoringType where name=?1 ")
    Long findIdByName(String name);

    List<MhMonitoringType> findByNameAndUnit(String name, String unit);

    List<MhMonitoringType> findByName(String name);

    List<MhMonitoringType> findByNameAndUnitIsNull(String name);

    List<MhMonitoringType> findByCategoryId(Long i);

    @Query(value = "SELECT * FROM wwyt_mh_monitoring_type WHERE CATEGORY_ID =?1 AND CODE =?2  AND delete_flag ='0'", nativeQuery = true)
    MhMonitoringType findByCategoryIdAndCode(long l, String pointTypeCode);

    @Query(value = "SELECT * FROM wwyt_mh_monitoring_type WHERE NAME =?1 AND UNIT =?2  AND ID!=?3 AND delete_flag ='0'", nativeQuery = true)
    List<MhMonitoringType> findByNameAndUnitAndIdNotEq(String name, String unit, Long id);

    @Query(value = "SELECT * FROM wwyt_mh_monitoring_type WHERE NAME =?1 AND ID!=?2 AND delete_flag ='0'", nativeQuery = true)
    List<MhMonitoringType> findByNameAndIdNotEq(String name, Long id);

    @Query(value = " select id as id, concat(NAME, '(', unit, ')') as dataTypeName " +
            " from wwyt_mh_monitoring_type " +
            " where DELETE_FLAG = '0' " +
            "  and TENENT_ID = ?1 " +
            "  and ORG_CODE like ?2% " +
            "  and UNIT is not null " +
            " union " +
            " select id as id, NAME as dataTypeName " +
            " from wwyt_mh_monitoring_type " +
            " where DELETE_FLAG = '0' " +
            "  and TENENT_ID = ?1 " +
            "  and ORG_CODE like ?2% " +
            "  and UNIT is null " +
            "order by id desc",nativeQuery = true)
    List<Tuple> getSimpleList(Long tenentId, String orgCode);
}
